/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 
 */
package net.martinimix.service.syndication.feedburner;

import java.io.IOException;
import java.io.InputStream;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 * Provides a FeedBurner service request response.
 * 
 * @author Scott Rossillo
 *
 */
public final class FeedBurnerResponse {
	
	private Document doc;
	
	/**
	 * Creates a new FeedBurner response.
	 * 
	 * @param responseBodyStream the <code>InputStream</code> containing the response body
	 * 
	 * @throws JDOMException if the <code>responseBodyStream</code> does not contain a valid XML
	 * document
	 * 
	 * @throws IOException if an error occurs while reading from the given 
	 * <code>responseBodyStream</code>
	 */
	protected FeedBurnerResponse(InputStream responseBodyStream) throws JDOMException, IOException {
		
		SAXBuilder builder = new SAXBuilder();
		doc = builder.build(responseBodyStream);
	}
	
	/**
	 * Returns true if the response is positive.
	 * 
	 * @return <code>true</code> if the response was positive;
	 * <code>false</code> otherwise
	 */
	public boolean isPositive() {
		
		return !"fail".equalsIgnoreCase(doc.getRootElement().getAttributeValue("stat"));
	}
	
	/**
	 * Returns the XML document contained in this response's body.
	 * 
	 * @return the response body as an XML <code>Document</code>
	 */
	public Document getResponseBody() {
		
		return doc;
	}
	
	/**
	 * Returns the FeedBurner feed id from the response if one was included.
	 * 
	 * @return
	 */
	public Long getFeedId() {
		
		String id = doc.getRootElement().getChild("feed").getAttributeValue("id");
		
		return id != null ? new Long(id) : null;
	}
	
	/**
	 * Raises an exception of the appropriate type if the response
	 * was not positive.
	 * 
	 * @see #isPositive()
	 */
	protected void raiseException() {
		
		Element error = doc.getRootElement().getChild("err");
		String errorCode;

		if(error == null) {
			return;
		}
		
		errorCode = error.getAttributeValue("code");
		
		if("1".equals(errorCode)) {
			throw new FeedBurnerUnknownUserException("FeedBurner login credentials invalid or missing!", this);
		} else if("5".equals(errorCode)) {
			throw new FeedBurnerFeedNotFoundException(error.getAttributeValue("msg"), this);
		} else if("9".equals(errorCode)) {
			throw new FeedBurnerMissingSourceException("FeedBurner source URL invalid or missing!", this);
		} else if("102".equals(errorCode)) {
			throw new FeedBurnerUriUnavailableException(error.getAttributeValue("msg"), this);
		} else {
			throw new FeedBurnerException("Uknown FeedBurner error occurred", this);
		}
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new XMLOutputter().outputString(doc);
	}
}